﻿using smforj.common.model;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;

namespace smforj.common.service
{
    /// <summary>
    /// 读取设备配置信息对象
    /// 该对象读取根目录下的XML文件并进行配置工作
    /// </summary>
    public class MonitorConfig
    {
        private static MonitorConfig instance;
        private static XmlDocument xmldoc; 
        private static XmlElement xmlelem;
        private static readonly String XMLDOCNAME = "Monitors.xml";
        private static String ConfigPath = AppDomain.CurrentDomain.BaseDirectory;

        static String rootname = "Monitors";
        static String monitor = "Monitor";
        static String ip = "ip";
        static String port = "port";
        static String location = "location";
        static String id = "id";
        static String type = "type"; 
        static String ctype = "ctype";

        private MonitorConfig(){
            //创建一个xml对象
            xmldoc = new XmlDocument(); 
        }
        /// <summary>
        /// 创建新的xml
        /// </summary>
        private static void CreateXml(String xmlpath)
        {
            //加入XML的声明段落,<?xml version="1.0" encoding="UTF-8"?>
            XmlDeclaration xmldecl;
            xmldecl = xmldoc.CreateXmlDeclaration("1.0", "UTF-8", null);
            xmldoc.AppendChild(xmldecl); 
            //加入一个根元素
            xmlelem = xmldoc.CreateElement("", rootname, "");
            xmldoc.AppendChild(xmlelem);
            //加入另外一个元素
            for (int i = 1; i < 3; i++)
            {

                XmlNode root = xmldoc.SelectSingleNode(rootname);//查找<Monitors> 
                XmlElement xe1 = xmldoc.CreateElement(monitor);//创建一个<monitor>节点 
                xe1.SetAttribute(ip, "127.0.0.1");//设置该节点ip属性 
                xe1.SetAttribute(port, "6789");//设置该节点prot属性  
                xe1.SetAttribute(location, "测试1");//设置该节点location属性 
                xe1.SetAttribute(id, Guid.NewGuid().ToString());//设置该节点id属性 
                xe1.SetAttribute(type, "0");//设置该节点id属性 
                xe1.SetAttribute(ctype, "0");//设置该节点ctype属性 
                root.AppendChild(xe1);//添加到<Monitors>节点中 
            }
            //保存创建好的XML文档
            xmldoc.Save(xmlpath); 
        
        }
 


        #region 供使用方法 

        /// <summary>
        /// 获取所有ConnConfig信息
        /// </summary>
        /// <returns></returns>
        public List<ConnConfig> GetConnConfig()
        {
            XmlNodeList nodeList = xmldoc.SelectSingleNode(rootname).ChildNodes;//获取Monitors节点的所有子节点 
            List<ConnConfig> list = new List<ConnConfig>();
            foreach (XmlNode xn in nodeList)//遍历所有子节点 
            {
                if (xn is XmlElement)
                {
                    XmlElement xe = (XmlElement)xn;//将子节点类型转换为XmlElement类型 
                    ConnConfig config = new ConnConfig();
                    config.Ip = xe.GetAttribute(ip);
                    config.Port = xe.GetAttribute(port);
                    config.Location = xe.GetAttribute(location);
                    config.Id = xe.GetAttribute(id);
                    config.Type = xe.GetAttribute(type);
                    config.Ctype = xe.GetAttribute(ctype);
                    list.Add(config);
                }
            }
            return list;
        }
 
        /// <summary>
        /// 初始化xml如果已经存在加载否则创建一个固定格式的新的xml
        /// </summary>
        /// <param name="xmlpath">文件路径</param>
        /// <returns></returns>
        public static MonitorConfig LoadOrCreate(String xmlpath)
        {
            //创建对象
            if (instance == null) instance = new MonitorConfig();
            //创建文件
            if (!File.Exists(xmlpath)) CreateXml(xmlpath); 
            xmldoc.Load(xmlpath);
            return instance;
        }

        /// <summary>
        /// 初始化xml如果已经存在加载否则创建一个固定格式的新的xml
        /// 则创建
        /// </summary>
        public static MonitorConfig LoadOrCreate()
        {
            //  如果是WIN7 ，则这个目录会有变化
            //if (Environment.OSVersion.Version.Major > 5)
            //{
            //    ConfigPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\Jztk\\Portal\\";

            //    if (!Directory.Exists(ConfigPath))
            //        Directory.CreateDirectory(ConfigPath);
            //}
            return LoadOrCreate(ConfigPath + @XMLDOCNAME);
        }
        #endregion
    } 

}
